// HTTP/3 header constants and utilities

pub const URL_HOST: &str = "hysteria";
pub const URL_PATH: &str = "/auth";

pub const REQUEST_HEADER_AUTH: &str = "Hysteria-Auth";
pub const RESPONSE_HEADER_UDP_ENABLED: &str = "Hysteria-UDP";
pub const COMMON_HEADER_CC_RX: &str = "Hysteria-CC-RX";
pub const COMMON_HEADER_PADDING: &str = "Hysteria-Padding";

pub const STATUS_AUTH_OK: u16 = 233;

use std::collections::HashMap;

pub type Headers = HashMap<String, String>;

pub fn auth_request_to_headers(auth: &str, rx: u64) -> Headers {
    let mut headers = Headers::new();
    headers.insert(REQUEST_HEADER_AUTH.to_string(), auth.to_string());
    headers.insert(COMMON_HEADER_CC_RX.to_string(), rx.to_string());
    headers
}

pub fn auth_response_to_headers(udp_enabled: bool, rx: u64, rx_auto: bool) -> Headers {
    let mut headers = Headers::new();
    headers.insert(RESPONSE_HEADER_UDP_ENABLED.to_string(), udp_enabled.to_string());
    
    if rx_auto {
        headers.insert(COMMON_HEADER_CC_RX.to_string(), "auto".to_string());
    } else {
        headers.insert(COMMON_HEADER_CC_RX.to_string(), rx.to_string());
    }
    
    headers
}

pub fn parse_auth_request(headers: &Headers) -> (String, u64) {
    let auth = headers.get(REQUEST_HEADER_AUTH).cloned().unwrap_or_default();
    let rx = headers.get(COMMON_HEADER_CC_RX)
        .and_then(|s| s.parse().ok())
        .unwrap_or(0);
    (auth, rx)
}

pub fn parse_auth_response(headers: &Headers) -> (bool, u64, bool) {
    let udp_enabled = headers.get(RESPONSE_HEADER_UDP_ENABLED)
        .and_then(|s| s.parse().ok())
        .unwrap_or(false);
    
    let rx_str = headers.get(COMMON_HEADER_CC_RX).map(|s| s.as_str()).unwrap_or("");
    let (rx, rx_auto) = if rx_str == "auto" {
        (0, true)
    } else {
        (rx_str.parse().unwrap_or(0), false)
    };
    
    (udp_enabled, rx, rx_auto)
}
